package com.health.db.dao.sm.impl;

import java.util.List;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.sm.IGoodsOrderDao;
import com.health.db.mapper.GoodsOrderMapper;
import com.health.db.model.GoodsOrder;
import com.health.db.model.GoodsOrderExample;
import com.health.redis.RedisConstants;

/**
 * @author YC
 *
 */
public class GoodsOrderDaoImpl extends ServiceInterface implements
		IGoodsOrderDao {

	
	
	GoodsOrderMapper goodsOrderMapper;
	String recId =":recId:";
	String userId=":userId";
	String goodsId=":goodsId";
	String goodsType=":goodsType";
	String  offset=":OF:";
	String  pageSize=":PS:";

	public GoodsOrderMapper getGoodsOrderMapper() {
		return goodsOrderMapper;
	}

	public void setGoodsOrderMapper(GoodsOrderMapper goodsOrderMapper) {
		this.goodsOrderMapper = goodsOrderMapper;
	}

	/**
	 *record 中含有ownerid(userId)
	 */
	@Override
	public int insert(GoodsOrder record) {
		// TODO Auto-generated method stub
		int i=goodsOrderMapper.insert(record);
		
		getRedisOperatiorLogic();
		
		String key=RedisConstants.GoodsOrderMapper+recId+record.getRecid();
		String keyList = RedisConstants.GoodsOrderMapper + userId+ record.getOwnerid().longValue() + goodsType	+ record.getGoodsType();
		String key2 = RedisConstants.GoodsOrderMapper+userId+record.getOwnerid().longValue()+goodsId +record.getGoodsid().longValue();
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(key, getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteKeys(keyList,  getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key2, getOrderRedisGroupNo("1"));
		}
		return  i;
	}

	@Override
	public int update(GoodsOrder record) {
		// TODO Auto-generated method stub
		
		int i=goodsOrderMapper.updateByPrimaryKeySelective(record);
		
		getRedisOperatiorLogic();
		String key=RedisConstants.GoodsOrderMapper+recId+record.getRecid();
		String key1 = RedisConstants.GoodsOrderMapper+userId+record.getOwnerid().longValue()+goodsId +record.getGoodsid().longValue();
		String key2 =  RedisConstants.GoodsOrderMapper+userId+record.getOwnerid().longValue()+goodsType+record.getGoodsType();
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(key, getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key1, getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(key2, getOrderRedisGroupNo("1"));
		}
		
		return  i;
	}

	@Override
	public GoodsOrder selectByPrimaryKey(Long recid) {
		// TODO Auto-generated method stub
		
		getRedisOperatiorLogic();
		String key=RedisConstants.GoodsOrderMapper+recId+recid;
		Object object=null;
		GoodsOrder  list=null;
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object = redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		}
		if(null!=object){
			
			if (!object.equals("")) {
				
			list =  (GoodsOrder) object;

			}
		}
		else
		{
			list=goodsOrderMapper.selectByPrimaryKey(recid);
			if(redisOperatiorLogic.isJedisIsConnected())
			{
			redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
			}
		}
		return list;
	}

	@Override
	public List<GoodsOrder> selectByUserId(GoodsOrderExample example) {
		// TODO Auto-generated method stub
		getRedisOperatiorLogic();
		String key = RedisConstants.GoodsOrderMapper+userId+example.getOwnerid().longValue()+goodsId +example.getGoodsid().longValue();
		Object object=null;
		 List<GoodsOrder> list=null;
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object = redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		}
		if(null!=object){
			
			if (!object.equals("")) {
				
			list =  ( List<GoodsOrder>) object;

			}
		}
		else
		{
			list=goodsOrderMapper.selectByUserId(example);
			if(redisOperatiorLogic.isJedisIsConnected())
			{
			redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
			}
		}
		return list;
	}
	
	/*
	 * 为减少误差 单独拆出goodstype项目
	 */
	@Override
	public List<GoodsOrder> selectByUserIdAndType(GoodsOrderExample example) {
		// TODO Auto-generated method stub
		getRedisOperatiorLogic();
		// redis key
		String key = RedisConstants.GoodsOrderMapper + userId
				+ example.getOwnerid().longValue() + goodsType
				+ example.getGoodsType() + offset + example.getOffset()
				+ pageSize + example.getPageSize();
		String keyList = RedisConstants.GoodsOrderMapper + userId+ example.getOwnerid().longValue() + goodsType	+ example.getGoodsType();
		Object object = null;
		List<GoodsOrder> list = null;
		if (redisOperatiorLogic.isJedisIsConnected()) {
			object = redisOperatiorLogic.readObject(key,
					getOrderRedisGroupNo("1"));
		}
		if (null != object) {

			if (!object.equals("")) {
				list = (List<GoodsOrder>) object;
			}
		} else {
			list = goodsOrderMapper.selectByUserId(example);
			if (redisOperatiorLogic.isJedisIsConnected()) {
				redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
				redisOperatiorLogic.lpush(keyList, key,getOrderRedisGroupNo("1"));
			}
		}
		return list;
	}
	
	
}
